package com.hb.guli.service.base.config;


import com.google.common.base.Predicates;
import lombok.Value;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.schema.AlternateTypeRule;
import springfox.documentation.schema.AlternateTypeRuleConvention;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

import static com.google.common.collect.Lists.newArrayList;
import static springfox.documentation.schema.AlternateTypeRules.newRule;

/**
 * @author jack
 * @date 2023/7/1 21:30
 * @description: swagger 配置文件
 */
@Configuration
@EnableSwagger2
@EnableWebMvc
public class Swagger2Config implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations(
                "classpath:/static/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
        WebMvcConfigurer.super.addResourceHandlers(registry);

    }

    //    @Value("${swagger.enabled}")
//    private Boolean enabled;

//    @Bean
//    public AlternateTypeRuleConvention pageableConvention(final TypeResolver resolver) {
//        return new AlternateTypeRuleConvention() {
//            @Override
//            public int getOrder() {
//                return Ordered.HIGHEST_PRECEDENCE;
//            }
//
//            @Override
//            public List<AlternateTypeRule> rules() {
//                return newArrayList(newRule(Page.class, IPage.class));
//            }
//        };
//    }

    @Bean
    public Docket webApiConfig() {
        return new Docket(DocumentationType.SWAGGER_2)
//                .enable(enabled)
                .groupName("webApi")
                .apiInfo(webApiInfo())
                .select()
                .paths(Predicates.and(PathSelectors.regex("/api/*"))).build();
//                .apiInfo(webApiInfo());
    }

    @Bean
    public Docket adminApiConfig() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("adminApi")
                .apiInfo(adminApiInfo())
                .select()
                .paths(Predicates.and(PathSelectors.regex("/admin/.*")))
                .build();
    }

    private ApiInfo webApiInfo() {
        return new ApiInfoBuilder()
                .title("API接口文档")
                .description("本文档描述了 guli 项目的 api 接口定义")
                .version("1.0")
                .contact(new Contact("jackhb1999","http://www.baidu.com","jack.h.b.fan@outlook.com"))
                .build();
    }

    private ApiInfo adminApiInfo() {
        return new ApiInfoBuilder()
                .title("后台管理的API接口文档")
                .description("本文档描述了 guli 项目的后端 api 接口定义")
                .version("1.0")
                .contact(new Contact("jackhb1999","http://www.baidu.com","jack.h.b.fan@outlook.com"))
                .build();
    }

}
